package com.carrentalplatform;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.io.File;

/**
 * 租车平台应用启动类
 */
@SpringBootApplication
@EnableTransactionManagement
@MapperScan("com.carrentalplatform.mapper")
public class CarRentalPlatformApplication {

    @Value("${file.upload.path:./uploads}")
    private String uploadPath;

    public static void main(String[] args) {
        SpringApplication.run(CarRentalPlatformApplication.class, args);
    }
    
    /**
     * 启动时创建上传目录
     */
    @Bean
    public CommandLineRunner init() {
        return args -> {
            // 创建主上传目录
            File uploadDir = new File(uploadPath);
            if (!uploadDir.exists()) {
                uploadDir.mkdirs();
                System.out.println("已创建上传根目录: " + uploadDir.getAbsolutePath());
            }
            
            // 创建车辆图片子目录
            File vehicleDir = new File(uploadPath + "/vehicle");
            if (!vehicleDir.exists()) {
                vehicleDir.mkdirs();
                System.out.println("已创建车辆图片目录: " + vehicleDir.getAbsolutePath());
            }
            
            // 输出上传目录的绝对路径，方便调试
            System.out.println("上传根目录: " + uploadDir.getAbsolutePath());
        };
    }
} 